from flask import Blueprint
from flask.views import MethodView
from models import User

user2_bp = Blueprint('user2', __name__, url_prefix='/admins/users/get_by_cardID')


class User2Api(MethodView):
    def get(self, user_cardID):
        # 无参
        if not user_cardID:
            users: [User] = User.query.all()
            results = [
                {
                    'user_login_cardID': user.cardID,
                    'user_login_username': user.username,
                    'user_login_password': user.password,
                    'user_login_role': user.role,
                    'user_login_money': user.money
                } for user in users
            ]
            return {
                'status': 'success',
                'message': '全部用户信息已返回',
                'results': results
            }
        # 有参,模糊查找
        users: [User] = User.query.filter(User.cardID.like('%' + user_cardID + '%')).all()
        results = [
            {
                'user_login_cardID': user.cardID,
                'user_login_username': user.username,
                'user_login_password': user.password,
                'user_login_role': user.role,
                'user_login_money': user.money
            } for user in users
        ]
        return {
            'status': 'success',
            'message': '用户信息查询成功',
            'results': results
        }


user2_view = User2Api.as_view('user2_api')
